# 导包
import pymysql

try:
    # 创建连接
    conn = pymysql.connect(host="211.103.136.244",
                           port=7061,
                           user="student",
                           password="iHRM_student_2021",
                           database="test_db",
                           charset="utf8")

    # 获取游标
    cursor = conn.cursor()

    # 执行sql（非查询操作）
    # 新增一条图书数据
    sql = "insert into t_book(id, title, pub_date) values(1314, '邋遢大王', '2020-03-03');"

    # # 将图书的阅读量+1
    # sql = "update t_book set `read` = `read` + 1 WHERE id = 555;"

    # # 删除测试的图书数据
    # sql = "delete from t_book where id = 555;"

    cursor.execute(sql)

    # 正常时，提交事务
    conn.commit()
except Exception as e:
    # 抛出异常
    print(e)
    # 异常时，回滚事务
    conn.rollback()
finally:
    # 关闭游标
    cursor.close()
    # 关闭连接
    conn.close()
